﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using AlexandriaPressPublisherBusinessLayer;

namespace AlexandriaPressWebsite.MasterPages
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int draftID = 0;
            try
            {
                NotificationLabel.Text = ProofreaderProcessManager.deadlineNotification(1/*Convert.ToInt32(Session["userID"])*/, ref draftID);
            }
            catch (Exception err)
            {
                throw err;
            }

            if (draftID > 0)
            {
                Session.Add("draftID", draftID);
            }
            else
            {
                ContentPlaceHolder content = (ContentPlaceHolder)this.Master.FindControl("formDoc");
                content.Visible = false;
            }
        }

        protected void uploadSubmitBtn_Click(object sender, EventArgs e)
        { 
                if (fileUploader.HasFile)
                {
                    if (fileUploader.FileName.Trim().Length > 100)
                    {
                        messageLabel.Text = "Please use a shorter file name.";
                    }
                    else
                    {
                        try
                        {
                            messageLabel.Text = ProofreaderProcessManager.reviewedDraftUpload(fileUploader.FileName, Server.MapPath("~\\Drafts\\" + fileUploader.FileName), fileUploader.PostedFile, 1/*Convert.ToInt32(Session["userID"])*/, Convert.ToInt32(Session["draftID"]));
                            if (messageLabel.Text == "File uploaded!")
                            {
                                NotificationLabel.Text = "You do not have any pending draft to review.";
                                uploadLabel.Visible = false;
                                fileUploader.Visible = false;
                                uploadSubmitBtn.Visible = false;
                            }
                        }
                        catch (Exception ex)
                        {
                            messageLabel.Text = "File could not be uploaded.";
                        }
                    }
                }         
        }
    }
}
